package com.china08.yxyapi.api.manage.tec.teaching.homework.myques;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkQuesGroup;
import com.china08.yxyapi.repository.mg.homework.HomeworkQuesGroupRelQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkQuesGroupRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class HomeworkGroupSchController extends YxyApiBaseController {

    @Autowired
    private HomeworkQuesGroupRepository homeworkQuesGroupRepository;

    @Autowired
    private HomeworkQuesGroupRelQuesRepository homeworkQuesGroupRelQuesRepository;

    @ApiOperation(value = "查询组")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/mysql/group/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {
        YxyProfile profile = getProfile(request, response);
        String schoolId = profile.getSchoolId();
        String teacherId = profile.getId();
        String phase = reqModel.phase;
        String subject = reqModel.subject;
        List<RespModel> respModels = new ArrayList<HomeworkGroupSchController.RespModel>();
        List<HomeworkQuesGroup> homeworkQuesGroups = homeworkQuesGroupRepository
                .findBySchoolIdAndTeacherIdAndStageAndSubject(schoolId, teacherId, phase, subject);
        for (HomeworkQuesGroup homeworkQuesGrou : homeworkQuesGroups) {
            RespModel model = new RespModel();
            model.id = homeworkQuesGrou.getId();
            model.name = homeworkQuesGrou.getGroupName();
            model.quesCount = homeworkQuesGroupRelQuesRepository.countByGroupId(model.id);
            respModels.add(model);
        }
        return respModels;
    }

    protected static class ReqModel {
        public String phase;
        public String subject;
    }

    protected static class RespModel {
        public String name;
        public String id;
        public Integer quesCount;

    }
}
